package main.Concurrent.Topology;

import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.concurrent.TimeUnit;

import com.googlecode.actorom.ThreadingPolicy;
import com.googlecode.actorom.Topology;
import com.googlecode.actorom.local.LocalTopology;
import com.googlecode.actorom.remote.ClientTopology;
import com.googlecode.actorom.remote.ServerTopology;

/**
 * @author Jared Hance
 *
 */
public class TopologyManager {
    private static TopologyManager singleton = null;
    private Topology topology;
    
    private final SecureRandom random = new SecureRandom();
    
    private TopologyManager() {
        this.topology = null;
    }
    
    public static TopologyManager getSingleton() {
        if(singleton == null) {
            singleton = new TopologyManager();
        }
        
        return singleton;
    }
    
    public void createLocal(String id, ThreadingPolicy policy) {
        topology = new LocalTopology(id, policy);
    }
    
    public void createServer(String host, int port, ThreadingPolicy policy) {
        topology = new ServerTopology(host, port, policy);
    }
    
    public void createClient(String host, int port, long communicationTimeout,
            TimeUnit timeoutUnit, long keepAlive, TimeUnit keepAliveUnit) {
        topology = new ClientTopology(host, port, communicationTimeout,
                timeoutUnit, keepAlive, keepAliveUnit);
    }
    
    public void shutdown() {
        topology.shutdown();
        topology = null;
    }
    
    public Topology getTopology() {
        return topology;
    }
    
    public String getRandomID(String base) {
        return base + "#" + new BigInteger(256, random);
    }
}
